home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 4 / The 640 Meg Shareware Studio CD-ROM Volume IV (Data Express)(1994).ISO / clang / rzsz0593.zip / MINIRB.C < prev    next >
C/C++ Source or Header  |  1993-05-02  |  3KB  |  147 lines

  1. char * Version = "minirb 3.00 05-02-93";
  2.  
  3. #include <stdio.h>
  4. #include <signal.h>
  5. #include <setjmp.h>
  6.  
  7. #define OK 0
  8. #define FALSE 0
  9. #define TRUE 1
  10. #define ERROR (-1)
  11. #define CAN ('X'&037)
  12. #define SOH 1
  13. #define STX 2
  14. #define EOT 4
  15. #define ACK 6
  16. #define NAK 025
  17. #define TIMEOUT (-2)
  18. #define RETRYMAX 15
  19. #define WCEOT (-10)
  20.  
  21. FILE *fout;
  22. long Bytesleft;
  23. int Blklen;
  24. char secbuf[1024];
  25. char linbuf[1024];
  26. int Lleft=0;
  27. jmp_buf tohere;
  28.  
  29. void mode(n) {
  30.  if (n) system("stty raw -echo");
  31.  else system("stty echo -raw");
  32. }
  33.  
  34. void alrm(c) { longjmp(tohere, -1); }
  35.  
  36. void bibi(n) {
  37.  mode(0); fprintf(stderr, "minirb: caught signal %d; exiting", n); exit(128+n);
  38. }
  39.  
  40. main() {
  41.  mode(1);
  42.  if (signal(SIGINT, bibi) == SIG_IGN) {
  43.   signal(SIGINT, SIG_IGN); signal(SIGKILL, SIG_IGN);
  44.  } else {
  45.   signal(SIGINT, bibi); signal(SIGKILL, bibi);
  46.  }
  47.  printf("minirb: Now send file(s) with \042sb file ...\042 command\r\n");
  48.  wcreceive(); mode(0); exit(0);
  49. }
  50.  
  51. wcreceive() {
  52.  for (;;) {
  53.   if (wcrxpn(secbuf) == ERROR) break;
  54.   if (secbuf[0]==0) return;
  55.   if (procheader(secbuf)==ERROR || wcrx()==ERROR) break;
  56.  }
  57. }
  58.  
  59. wcrxpn(rpn) char *rpn; {
  60.  register c;
  61.  
  62. et_tu:
  63.  sendline(NAK); Lleft=0;
  64.  while ((c = wcgetsec(rpn)) != 0) {
  65.   if (c == WCEOT) { sendline(ACK); Lleft=0; rdln(2); goto et_tu; }
  66.   return ERROR;
  67.  }
  68.  sendline(ACK); return OK;
  69. }
  70.  
  71. wcrx() {
  72.  register int sectnum, sectcurr, sendchar, cblklen;
  73.  
  74.  sectnum=0; sendchar=NAK;
  75.  for (;;) {
  76.   sendline(sendchar); Lleft=0;
  77.   sectcurr=wcgetsec(secbuf);
  78.   if (sectcurr==(sectnum+1 & 0377)) {
  79.    sectnum++; cblklen = Bytesleft>Blklen ? Blklen:Bytesleft;
  80.    putsec(secbuf, cblklen);
  81.    if ((Bytesleft-=cblklen) < 0) Bytesleft = 0;
  82.    sendchar=ACK;
  83.   }
  84.   else if (sectcurr==(sectnum&0377)) sendchar=ACK;
  85.   else if (sectcurr==WCEOT) {
  86.    if (fclose(fout)==ERROR) return ERROR;
  87.    sendline(ACK); Lleft=0; return OK;
  88.   }
  89.   else if (sectcurr==ERROR) return ERROR;
  90.   else return ERROR;
  91.  }
  92. }
  93.  
  94. wcgetsec(rxbuf) char *rxbuf; {
  95.  register checksum, wcj, firstch; register char *p; int sectcurr, errors;
  96.  for (errors=0; errors<RETRYMAX; errors++) {
  97.   if ((firstch=rdln(5))==STX) { Blklen=1024; goto get2; }
  98.   if (firstch==SOH) {
  99.    Blklen=128;
  100. get2:
  101.    sectcurr=rdln(2); checksum=0;
  102.    if ((sectcurr+(rdln(2)))==0377) {
  103.     for (p=rxbuf,wcj=Blklen; --wcj>=0; ) {
  104.      if ((firstch=rdln(2)) < 0) goto bilge;
  105.      checksum += (*p++ = firstch);
  106.     }
  107.     if ((firstch=rdln(2)) < 0) goto bilge;
  108.     if (((checksum-firstch)&0377)==0) return sectcurr;
  109.    }
  110.   }
  111.   else if (firstch==EOT) return WCEOT;
  112.   else if (firstch==CAN) return ERROR;
  113. bilge:
  114.   while(rdln(2)!=TIMEOUT)
  115.    ;
  116.   sendline(NAK); Lleft=0;
  117.  }
  118.  return ERROR;
  119. }
  120.  
  121. rdln(timeout) int timeout; {
  122.  static char *cdq;
  123.  
  124.  if (--Lleft >= 0) return (*cdq++ & 0377);
  125.  if (setjmp(tohere)) { Lleft = 0; return TIMEOUT; }
  126.  signal(SIGALRM, alrm); alarm(timeout);
  127.  Lleft=read(0, cdq=linbuf, 1024); alarm(0);
  128.  if (Lleft < 1) return TIMEOUT;
  129.  --Lleft; return (*cdq++ & 0377);
  130. }
  131.  
  132. procheader(name) char *name; {
  133.  register char *p;
  134.  
  135.  Bytesleft = 2000000000L; p = name + 1 + strlen(name);
  136.  if (*p) sscanf(p, "%ld", &Bytesleft);
  137.  if ((fout=fopen(name, "w")) == NULL) return ERROR;
  138.  return OK;
  139. }
  140.  
  141. putsec(p, n) char *p; int n;
  142. { for (; --n>=0; ++p) putc(*p, fout); }
  143.  
  144. sendline(c) { char d; d = c; write(1, &d, 1); }
  145.  
  146. /* End of minirb.c */
  147.